@use '../config';

// 混合颜色
// $color 需要被减淡的颜色
// $weight 被减淡的颜色的权重
@function mix-color($color, $weight, $mix-color: #fff) {
  @return mix($color, $mix-color, $weight);
}

// 计算函数
@function big($a, $b: 1, $operator:'plus') {
  $result: '';

  @if $operator == 'plus' {
    $result: calc($a + $b);
  }
  @else if $operator == 'minus' {
    $result: calc($a - $b);
  }
  @else if $operator == 'times' {

    @if $b == 0 {
      $b: 1;
    }

    $result: calc($a * $b);
  }
  @else if $operator == 'div' {

    @if $b == 0 {
      $b: 1;
    }

    $result: calc($a / $b);
  }
  @else {
    $result: $a;
  }

  @return $result;
}

// 给类名打上连接符
@function connector-decorator($list...) {
  $result: '';
  $list-len: 0;
  $new-list: ();

  $_index: 1;

  @while $_index <= length($list) {

    $value: nth($list: $list, $n: $_index);

    @if $value != '' and $value != DEFAULT {
      $new-list: append(
        $list: $new-list,
        $val: $value,
        $separator: comma
      );
    }

    $_index: big($_index);

  }

  $list-len: length($list: $new-list);

  $separator: '';

  $separators: config.$class-separators;

  @if length($list: $separators) < 1 {
    $separators: ('-')
  }

  $separator: nth($list: $separators, $n: 1);

  $separators-len: length($list: $separators);

  $index: 1;

  @while $index <= $list-len {

    @if $separators-len > 0 {

      @if $index <= $separators-len {
        $separator: nth($list: $separators, $n: $index);
      }

    }

    @if $index == $list-len {
      $separator: '';
    }

    $result: $result + nth($list: $new-list, $n: $index) + $separator;

    $index: big($index);

  }

  @return $result;
}

@function class-generator($classes...) {
  $selector: '.';

  $prefix: config.$class-prefix;

  $new-classes: ();

  $first-class: nth($list: $classes, $n: 1);

  $tag: '';

  $index: 1;

  // 兼容负数
  @if $first-class == '-' {
    $tag: $first-class;

    $index: 2;
  }

  @while $index <= length($classes) {
    $new-classes: append(
      $list: $new-classes,
      $val: nth($list: $classes, $n: $index),
      $separator: comma
    );

    $index: big($index);
  }

  @return
    $selector +
    $tag +
    connector-decorator(
      $prefix,
      $new-classes...
    );
}

// 带透明度的颜色混合
@function lighter-color($color, $opacity) {
  @return mix-color($color, percentage(calc($opacity / 10 / 100)));
}

@function backslash-escape($value) {
  @if type-of($value: $value) == 'string' {

    $dot-index: str-index($value, '.');

    $slash-index: str-index($value, '/');

    @if $dot-index !=null {
      @return unquote($string: str-insert(quote($string: $value), '\\', $dot-index));
    }

    @if $slash-index !=null {
      @return unquote($string: str-insert(quote($string: $value), '\\', $slash-index));
    }

    @return unquote($string: $value);

  }

  @return $value;
}

// 处理字典数据，对特殊字符进行转义
@function map-handler($value, $ext-map: null) {

  @if $value !=''and $value !=null and length($list: $value)>0 {
    $keys: map-keys($map: $value);
    $values: map-values($map: $value);
    $index: 1;

    $result: (backslash-escape(nth($list: $keys, $n: $index)): nth($list: $values, $n: $index));

    $index: $index + 1;

    @while $index <=length($list: $value) {
      $result: map-merge($map1: $result, $map2: (backslash-escape(nth($list: $keys, $n: $index)): nth($list: $values, $n: $index)));

      $index: $index + 1;
    }

    @if $ext-map !=null and length($list: $ext-map)>0 {
      $result: map-merge($map1: $result, $map2: $ext-map);
    }

    @return $result;
  }

  @return ();
}

@function expect($class-name) {
  @return if(($class-name != null and $class-name != false), true, false)
}

// 颜色明度数组
$map-color-brightness: (
  '50': 50,
  '100': 100,
  '200': 200,
  '300': 300,
  '400': 400,
  '500': 500,
  '600': 600,
  '700': 700,
  '800': 800,
  '900': 900
);